
//This servlets generates the list of broadcasters.
// Code written by Shreyas Kamath

import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

public class BroadCasterServlet extends HttpServlet{

	//Vector that adds the name of the broadcasters
	public static Vector vector_Broadcastername =new Vector(20,5);
	public static int iNumBroadcasters=0;	
	
	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("FriendsServlet: init()");
	}
	
	//service method of the servlet.
	public void doGet(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		int ibroadcasterno;
		// set the response type
		string strNym=request.getParameter("nym");
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");		
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		this .GetBroadcasterNames(strNym);
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Broadcaster>");
		for( ibroadcasterno=0; ibroadcasterno < iNumBroadcasters; ibroadcasterno++ )
		{	
			out.println("<Broadcastername>");
			out.println( vector_Broadcastername.elementAt(ibroadcasterno));
			out.println("</Broadcastername>");
		}
		out.println("</Broadcaster>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;
		
		try
		{
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			// Do nothing
		};  
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetBroadcasterNames()
	//This method gets the name of the broadcasters subscribed to the nym
	// stores the list in the vector for broadcasters.
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	public static void GetBroadcasterNames(String strNym )
	{
		ResultSet rsBroadcasterName;
		String strBroadCasterName;
		Connection conn=Connect();
		try
		{
			String strNymID=null;
			if(strNym != null)
			strNymID=GetNymID(strNymName);
			Statement getBroadCasterName=conn.createStatement();
			 //Get the names of the broadcasters.
			if(strNymID==null)
			rsBroadcasterName=getBroadCasterName.executeQuery("select BroadCasterName,BroadCasterID from broadcasters order by ExpiryDate desc"); 
			else
			rsBroadcasterName=getBroadCasterName.executeQuery("select BroadCasterName,broadcasters.BroadCasterID from broadcasters,broadcasternymmap where broadcasters.BroadCasterID=broadcasternymmap.BroadCasterID and broadcasternymmap.NymID="+strNymID+" order by ExpiryDate desc"); 
			while ( rsBroadcasterName.next() )
			{
				strBroadCasterName=rsBroadcasterName.getString("BroadCasterName");
				//Add the broadcasters name to the vector
				vector_Broadcastername.addElement(strBroadCasterName);
				System.out.println("Broad CasterName:"+strBroadCasterName);
				iNumBroadcasters++;
			}	
			
		}
		catch(Exception e)
		{
			System.out.println(" The following Exception occured"+e);
		}
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function GetBroadCasterNames()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetNymID()
	// Gets the 128 bit NymID corresponding to a nym using the nym name as the parameter
	// returns 128 bit NymID
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	public static String GetNymID(String strNym)
	{
		Connection conn =Connect();
		String strNymid=null;
		ResultSet rsNymID;
		try
		{
			Statement getNymID= conn.createStatement();
		      //Get the nymid corresponding to the nym name from the database 
			rsNymID=getNymID.executeQuery("select nymid from nymnamemap where nymname='" + strNym + "'");
			while( rsNymID.next() )
			{
				strNymid=rsNymID.getString("nymid");
			}
		}
		catch( Exception e )
		{
			System.out.println ( "Database Exception is " + e );
		};  
		return(strNymid);
	}	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function GetNymID()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
		
	
	public void destroy() {
		System.out.println("BroadCasterServlet: destroy()");
	}
}